package classifier;

import java.io.FileNotFoundException;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.io.*;

public class DTFile {
	private String filepath;
	private FeatureType ft;
	private int countFeatures;
	private String[] featureNames;
        private HashMap<Item, String> trainingSet = new HashMap<Item, String>();
        private HashMap<String, FeatureType> featureMap = new HashMap<String, FeatureType>();	
	//private 
	public DTFile(String filepath, String[] featureNames){
		this.filepath = filepath;
		this.featureNames = featureNames;
	}
	
	public DecisionTree buildTree(){
			File file = new File(filepath);
			Scanner lines = null;
			try {
				lines = new Scanner(file);
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			//lines.useDelimiter(";");
			String line = lines.nextLine();
			//System.out.println("First line:" + line);
			String[] splitline = line.split(";");
			int featureCount = Integer.parseInt(splitline[1]);
			line = lines.nextLine();
			splitline = line.split(";");
			int itemCount = Integer.parseInt(splitline[1]);
			
			FeatureType ft = new FeatureType("yn", new String[]{"0", "1"});
			
			while(lines.hasNext()){
				line = lines.nextLine();
				splitline = line.split(";");
				String itemName = splitline[0];
				String[] featureValues = new String[featureNames.length];
				Feature[] features = new Feature[featureNames.length];
				for(int i = 0; i < features.length; i++){
					featureValues[i] =  splitline[i + 1];
					//System.out.println("featureValues: " + featureValues[i]);
				}

				
				for(int i = 0; i < features.length; i++){
					features[i] = new Feature(featureNames[i], featureValues[i], ft);

				}

				trainingSet.put(new Item(itemName, features), splitline[splitline.length - 1]);

				
				
			}

			for(int i = 0; i<featureNames.length;i++){
                        featureMap.put(featureNames[i],ft);
 
			
			}
			DecisionTree tree = new DecisionTree(trainingSet, featureMap);
			return tree;

	}
}
